package com.smartcat.web.actions.selfservice;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.smartcat.bean.LoginUserBean;
import com.smartcat.model.UserFunctionMap;
import com.smartcat.model.UserFunctionMapKey;
import com.smartcat.service.LoadDataService;
import com.smartcat.service.SelfService;
import com.smartcat.web.BaseAction;

@Controller
public class OrderFunctionAction extends BaseAction{

	private SelfService selfService;
	private LoadDataService loadDataService;
	
	private Integer funcId;
	
	private String resultMsg;

	@Autowired
	public void setSelfService(SelfService selfService) {
		this.selfService = selfService;
	}
	
	@Autowired
	public void setLoadDataService(LoadDataService loadDataService) {
		this.loadDataService = loadDataService;
	}


	public String getResultMsg() {
		return resultMsg;
	}

	public void setResultMsg(String resultMsg) {
		this.resultMsg = resultMsg;
	}

	public void setFuncId(Integer funcId) {
		this.funcId = funcId;
	}

	public String addFunction(){
		Object obj=ActionContext.getContext().getSession().get("loginUser");
		if(obj==null){
			return ERROR;	
		}else{
			LoginUserBean user=(LoginUserBean)obj;
				// add new function
					UserFunctionMap userFunctionMap=new UserFunctionMap();
					userFunctionMap.setUserId(user.getUserId());
					userFunctionMap.setFunctionId(funcId);
					userFunctionMap.setStartDate(new Date());
					userFunctionMap.setStatus(0);
					selfService.saveUserFunctionMap(userFunctionMap);
			}
		resultMsg="ok";
		return SUCCESS;
	}
	
	public String removeFunction(){
		Object obj=ActionContext.getContext().getSession().get("loginUser");
		if(obj==null){
			return ERROR;	
		}else{
		LoginUserBean user=(LoginUserBean)obj;
		UserFunctionMapKey userFunctionMapKey=new UserFunctionMapKey();
		userFunctionMapKey.setFunctionId(funcId);
		userFunctionMapKey.setUserId(user.getUserId());
		selfService.deleteUserFunctionMap(userFunctionMapKey);
		}
		resultMsg="ok";
				
		return SUCCESS;
	}
	
}
